﻿using Dict.Config;
using System;
using System.Collections.Generic;
using Dict.DataModel;

namespace Battle.Engine.LogicModel
{
    class BuffOfDelayCreate : Buff {
        string[] buffs;
        List<Buff> createBuffs;
        public override Buff Clone()
        {
            var newBuff = base.Clone() as BuffOfDelayCreate;
            if (newBuff.buffs == null)
            {
                newBuff.buffs = param.Split(',');
            }
            if (newBuff.createBuffs == null)
            {
                newBuff.createBuffs = new List<Buff>();
            }
            foreach (var buff in newBuff.buffs)
            {
                Buff b = FBuffFactory.CreateBuff(buff, this.engineData);
                newBuff.createBuffs.Add(b.Clone());
            }
            return newBuff;
        }
        public override void Init(TableBuff tableBuff, EngineData engineData)
        {
            base.Init(tableBuff, engineData);


        }

        public override void LoadBuff0()
        {

        }

        public override void UnloadBuff0()
        {

        }
        public override void Update0(EngineData data)
        {
            if(args1 > 0)
            {//延迟时间
                if ((data.roundTime - this.loadBuffTime) >= this.args1)
                {//延迟时间到
                    Effect();
                    UnloadBuff();
                }
            }
        }

        private bool isRoundEnd = false;
        public override void minusRound0()
        {
            if(args2 > 0)
            {//延迟回合
                if((this.engineData.roundNum - this.loadBuffRound) >= args2)
                {//回合次数到
                    Effect();
                    UnloadBuff();
                }
            }
            //回合到了，改回合的时间延迟buff直接生效
            if (args1 > 0)
            {
                isRoundEnd = true;
                Effect();
                isRoundEnd = false;
                UnloadBuff();
            }
        }

        public override void Effect()
        {
            switch (this.factor)
            {
                case TableFactorConst.delayCreateBuffs:
                    FExportOnceData exportOnceData = new FExportOnceData();
                    exportOnceData.Init(this);
                    foreach (var buff in createBuffs)
                    {
                        if (isRoundEnd)
                        {
                            if (buff.totalRound > 0)
                                buff.totalRound--;
                            //直接过滤非持续性BUFF 不做添加
                            if (buff.totalRound <= 0 && buff.attCount <= 0 && buff.defCount <= 0)
                                continue;
                        }
                        if (this.buffOrigion.GetBuffSourceClass() == BuffSourceClass.SKILLCAST)
                        {
                            SkillCastBuffOrigin origin = (SkillCastBuffOrigin)this.buffOrigion;
                            this.buffTargetActor.buffManager.LoadSkillBuffBuff(origin.sourceSkillCast, buff, this.timer, exportOnceData);
                        }
                        if(this.buffOrigion.GetBuffSourceClass() == BuffSourceClass.HERO)
                        {
                            TalentBuffOrigin origin = (TalentBuffOrigin)this.buffOrigion;
                            this.buffTargetActor.buffManager.LoadTalentBuffBuff(origin.sourceTalent, buff, this.timer, exportOnceData);
                        }
                    }
                    this.engineData.exports.Add(exportOnceData);
                    break;
                default:
                    break;
            }
        }

        public override void UnEffect()
        {

        }

    }
}
